<template>
  <div>
    <h3>商品分类</h3>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品分类</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片视图区域 -->
        <el-card>
            <!-- 添加分类按钮区域 -->
          <el-button type="primary" @click="addCate">添加分类</el-button>
          <tree-grid :show-row-hover="false" :show-index="true" index-text="#" border :expand-type="false" :selection-type="false" :data="cates" :columns="columns">
            <template slot-scope="scope" slot="isOK">
              <i v-if="!scope.row.cat_deleted" class="el-icon-success" style="color: green"></i>
              <i v-else class="el-icon-error" style="color: red"></i>
            </template>
            <template slot="order" slot-scope="scope">
              <el-tag v-if="scope.row.cat_level==0">一级</el-tag>
              <el-tag  v-else-if="scope.row.cat_level==1" type="success">二级</el-tag>
              <el-tag v-else type="warning">三级</el-tag>
            </template>
            <template slot="opt" >
              <el-button size="mini" type="primary" icon="el-icon-edit" round>编辑</el-button>
              <el-button size="mini" type="danger" icon="el-icon-delete" round>删除</el-button>
            </template>
          </tree-grid>
          <!-- 分页区域 -->
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryData.pagenum"
            :page-sizes="[5, 10, 20, 40]"
            :page-size="queryData.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </el-card>
        <!-- 添加分类对话框 -->
        <el-dialog
          @close="closeAddCate"
          title="添加分类"
          :visible.sync="addCatedialogVisible"
          width="50%">
          <el-form ref="addCateFormRef" :model="addCateForm" :rules="rules"  label-width="100px">
            <el-form-item prop="cat_name" label="分类名称：">
              <el-input v-model="addCateForm.cat_name"></el-input>
            </el-form-item>
            <el-form-item label="父级名称：">
              <el-cascader
                v-model="cascaderValue"
                :options="parentCate"
                :props="cascaderProps"
                @change="cascaderHandleChange">
              </el-cascader>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="addCatedialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="submitAddCate">确 定</el-button>
          </span>
        </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      queryData: {
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      cates: [],
      // 配置树形表格每一列的配置
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          // 定义具名插槽的名字
          template: 'isOK'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt'
        }
      ],
      addCatedialogVisible: false,
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      rules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
        ]
      },
      parentCate: [],
      cascaderValue: [],
      cascaderProps: {
        expandTrigger: 'hover',
        label: 'cat_name',
        value: 'cat_id',
        checkStrictly: true
      }
    }
  },

  created () {
    this.getGoodsCate()
  },

  methods: {
    async getGoodsCate () {
      const { data: res } = await this.$axios.get('categories', {
        params: this.queryData
      })
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.total = res.data.total
      this.cates = res.data.result
    },
    handleSizeChange (val) {
      this.queryData.pagesize = val
      this.getGoodsCate()
    },
    handleCurrentChange (val) {
      this.queryData.pagenum = val
      this.getGoodsCate()
    },
    async addCate () {
      this.addCatedialogVisible = true
      const { data: res } = await this.$axios.get('categories', {
        params: {
          type: 2
        }
      })
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.parentCate = res.data
    },
    cascaderHandleChange () {
      // console.log(111)
      if (this.cascaderValue.length > 0) {
        this.addCateForm.cat_level = this.cascaderValue.length
        this.addCateForm.cat_pid = this.cascaderValue[this.cascaderValue.length - 1]
        console.log(this.addCateForm.cat_pid)
      } else {
        this.addCateForm.cat_level = 0
        this.addCateForm.cat_pid = 0
      }
    },
    closeAddCate () {
      // 重置表单数据
      this.$refs.addCateFormRef.resetFields()
      // 清除addCateForm数据
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
      // 清除级联选择器保存的数据
      this.cascaderValue = []
    },
    submitAddCate () {
      this.$refs.addCateFormRef.validate(async flag => {
        if (!flag) return this.$message.error('填写分类名称')
        const { data: res } = await this.$axios.post('categories', this.addCateForm)
        if (res.meta.status !== 201) return this.$message.error('添加失败')
        this.addCatedialogVisible = false
        this.getGoodsCate()
        this.$message.success('添加成功')
      })
    }
  }
}
</script>

<style scoped lang='less'>
.zk-table {
  margin: 15px 0;
  font-size: 16px;
}

.el-cascader {
  width: 100%;
}
</style>
